<?php
function nf_excerpt($text){
    return str_replace('[...]', '...<br /><a class="more" href="'.get_permalink($post->ID).'">'.'Citeşte tot'.'</a>', $text);
}
add_filter('the_excerpt', 'nf_excerpt');
function nf_content($content){
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	$subs = array();
	
	foreach ($matches[1] as $key =>$v0) {
		$subs['@(?:<p>)*\s*\<img.+src="'. $v0 .'".*>\s*(?:</p>)*@i']  = '<a class="mb" id="mb'. $key .'" href="'. $v0 .'" rel="[images]"><img src="'. $v0 .'" /></a><div class="MultiBoxDescription mbHidden mb'. $key .'"><!-- --></div>';
		//$subs['@\<img.+src="'. $v0 .'".*>@i']  = '<a class="mb" id="mb'. $key .'" href="'. $v0 .'" rel="[images]"><img src="'. $v0 .'" /></a><div class="MultiBoxDescription mbHidden mb'. $key .'"><!-- --></div>';
	}
	
	$content = preg_replace(array_keys($subs), array_values($subs), $content);
	return $content;
//	echo "<pre>"; print_r ($matches); echo "</pre>";
}
add_filter('the_content', 'nf_content');
function gravatar($rating = false, $size = false, $email) {
	global $comment;
	$border = false;
	$default = false;
	
	$out = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email);
	if($rating && $rating != '')
		$out .= "&amp;rating=".$rating;
	if($size && $size != '')
		$out .="&amp;size=".$size;
	if($default && $default != '')
		$out .= "&amp;default=".urlencode($default);
	if($border && $border != '')
		$out .= "&amp;border=".$border;
	echo $out;
}
function nf_banner_ads() {
    global $wpdb;

    $gid = '1';

    $response = '';
    $images = $wpdb->get_results("
			SELECT
				".$wpdb->prefix."ngg_pictures.filename as _file,
				".$wpdb->prefix."ngg_gallery.path as _path,
				".$wpdb->prefix."ngg_pictures.description as _desc
			FROM
				".$wpdb->prefix."ngg_gallery
			INNER JOIN 
				".$wpdb->prefix."ngg_pictures 
			ON 
				".$wpdb->prefix."ngg_gallery.gid = ".$wpdb->prefix."ngg_pictures.galleryid
			WHERE 
				".$wpdb->prefix."ngg_gallery.gid = '".$gid."'
			AND
				".$wpdb->prefix."ngg_pictures.exclude = 0		
			LIMIT 5	
		", ARRAY_A);

    $response .= '
		    <div id="slideshow">    
		      <span id="loading">Loading</span>
		';
    if ($images != null) {
        $response .= "<ul id='pictures'>";

        foreach ($images as $key=>$value) {
            $response .= '<li><img src="'.get_bloginfo('url')."/".$value["_path"]."/".$value["_file"].'" alt="'.$value["_desc"].'" title="'.$value["_desc"].'" /></li>';
        }
        $response .= "</ul><ul id='menu'>";
        foreach ($images as $key=>$value) {
            $response .= '<li><a href="'.get_bloginfo('url')."/".$value["_path"]."/".$value["_file"].'">'.$value["_desc"].'</a></li>';
        }
        $response .= "</ul>";
    }

    $response .= "</div>";
    echo $response;
}
function nf_daily_art() {
	global $post; global $wp_query;
	
		$art = new WP_Query("cat=8&showposts=1");
		//echo "<pre>"; print_r ($art); echo "</pre>";
		while ($art->have_posts()) : $art->the_post();
			$str = wpautop($post->post_content);
?>				
			<h2><a href="<?php the_permalink()?>"><?php the_title()?></a></h2>
			<!-- <small><?php the_time('j F Y') ?>  by <?php the_author() ?> </small> -->
			<div class="entry">
				<a id="mb<?php the_ID()?>" class="mb" rel="[images]" href="<?php echo catch_that_image($str) ?>">
					<img src="<?php echo catch_that_image($str) ?>" alt="<?php the_title()?>" />
				</a>
				<div class="MultiBoxDescription mbHidden mb<?php the_ID()?>"><?php the_title()?></div>
				<small><?php the_time('j F Y') ?>  by <?php the_author() ?> </small><br /><br />
				<?php the_excerpt();?>
				<span class="postcom"><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></span>
				<div class="clearer"><!-- --></div>
			</div>	
<?php
		endwhile;
}
function nf_home_cat($iCat, $iShow) {
	global $post; global $wp_query;
	
		$art = new WP_Query("cat=". $iCat ."&showposts=".$iShow);
		$catlink = get_category_link($iCat);
		$catname = get_cat_name($iCat);
		//echo "<pre>"; print_r ($art); echo "</pre>";
?>
		<h1><a href="<?php echo $catlink;?>" ><?php echo $catname;?></a></h1>

<?php 		
		while ($art->have_posts()) : $art->the_post();
?>
		<div class="item" id="post-<?php the_ID(); ?>">
			<div class="post-photo">		
	<?php
			if (function_exists('gravatar')) { ?>
			<img src="<?php gravatar("R", 70, get_the_author_email()); ?>" alt="<?php the_author() ?>"/>
			<?php }?>
			</div>
			<div class="post-meta">		
				<h2><?php the_author();?></h2>
				<h3><a href="<?php the_permalink()?>"><?php the_title()?></a></h3>
				<small><?php the_time('j F Y') ?> by <?php the_author() ?></small>			 
			</div>	
			<div class="clearer"><!-- --></div>
			<div class="entry">
				<?php the_excerpt();?>
				<span class="postcom"><?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></span>
				<div class="clearer"><!-- --></div>
			</div>	
		</div>	
<?php
		endwhile;
}
function catch_that_image($content) {
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
    $first_img = $matches[1][0];
	
	//echo "<pre>"; print_r ($matches); echo "</pre>";

    if (empty($first_img)) { //Defines a default image
        $first_img = "/img/default.png";
    }
    return $first_img;
}
function nf_related_posts($iPost){
	$tags = wp_get_post_tags($iPost);
	if ($tags) {
		$first_tag = $tags[0]->term_id;
		$args=array(
			'tag__in' => array($first_tag),
			'post__not_in' => array($iPost),
			'showposts'=>5,
			'caller_get_posts'=>1
		);
		$my_query = new WP_Query($args);
		if( $my_query->have_posts() ) {
			?>
			<ul class="related-posts">
			<h3>Related Posts</h3>
				<?php
				while ($my_query->have_posts()) : $my_query->the_post(); ?>
				  <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
				  <?php
				endwhile;
				?>
			</ul>	
		<?php	
	  	}
	}	
}
function nf_popular_posts(){
	global $wpdb;
?>	
	<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title,post_content FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
	foreach ($result as $post) {
	    setup_postdata($post);
	    $postid = $post->ID;
	    $title = $post->post_title;
		$content = $post->post_content;
		
		$excerpt_length = 55;

		$words = explode(' ', $content, $excerpt_length + 1);
		if(count($words) > $excerpt_length) :
			array_pop($words);
			array_push($words, '...<br /><a class="more" href="'.get_permalink($postid).'">'.'Citeşte tot'.'</a>');
			$content = implode(' ', $words);
		endif;
		$content = preg_replace("/<img[^>]+\>/i", "", $content);
		$content = '<p>' . $content . '</p>';
		
	    $commentcount = $post->comment_count;
		if ($commentcount != 0) { ?>
			<h2><span class="png"><!-- --></span><a href="<?php echo get_permalink($postid); ?>" rel="bookmark" title="<?php echo $title ?>"><?php echo $title ?> [<?php echo $commentcount ?>]</a></h2>							
			<div class="entry"><?php echo $content; ?></div>
		<?php } } ?>
<?php
}
function nf_recent_comments(){
	global $wpdb;
	
	$show_pass_post = false;
	$no_comments = 5;
	$excerpt_length = 45;
	
	    $request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_email, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
		if(!$show_pass_post) $request .= "AND post_password ='' ";
		$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT ". $no_comments;
		$comments = $wpdb->get_results($request);	
?>
<?php  
  foreach ($comments as $comment) {
	$words = explode(' ', $comment->comment_content, $excerpt_length + 1);
	if(count($words) > $excerpt_length){
		array_pop($words);
		array_push($words, '...<br /><a class="more" href="'.get_permalink($comment->ID)."#comment-" . $comment->comment_ID.'">'.'Citeşte tot'.'</a>');
		$content = '<p>' . implode(' ', $words) . '</p>';
	} else {
		$content = '<p>' . $comment->comment_content . '...<br /><a class="more" href="'.get_permalink($comment->ID)."#comment-" . $comment->comment_ID.'">'.'Citeşte tot'.'</a>' .'</p>';
	}
?>
	<?php if (function_exists('gravatar')) { ?>
		<h2>
			<span class="png"><!-- --></span>
			<img src="<?php gravatar("R", 25, $comment->comment_author_email); ?>" alt="<?php $comment->comment_author ?>"/>
			<?php } ?>
			<big><?php echo strip_tags($comment->comment_author) ?></big>
			<small><a href="<?php echo get_permalink($comment->ID)."#comment-" . $comment->comment_ID ?>" title="<?php echo $comment->post_title?>"><?php echo $comment->post_title; ?></a></small>
		</h2>	
		<div class="entry">
			<?php echo $content ?>
		</div>
<?  	
  }
}
function getfeeds($fId){
	require_once (ABSPATH.WPINC.'/rss-functions.php');
	$url = 'http://pipes.yahoo.com/pipes/pipe.run?_id='. $fId .'&_render=rss';
	$rss = fetch_rss($url);
	$excerpt_length = 15;

	if ($rss) {
		?>
		<ul id="rss-feeds">
		<?php
//		echo "<pre>"; print_r($rss); echo "</pre>";
	    foreach ($rss->items as $item) {
	        $href = $item['link'];
	        $title = $item['title'];
	        $pubdate = $item['pubdate'];
			$description = preg_replace("/<img[^>]+\>/i", "", $item['description']);
			
    		$words = explode(' ', $description, $excerpt_length + 1);
				if(count($words) > $excerpt_length){
					array_pop($words);
					array_push($words, ' [...]');
					$content = '<p>' . implode(' ', $words) . '</p>';
				} else {
					$content = '<p>' . $description . ' [...]' .'</p>';
				}
		?>
			<li><h3><a target="_blank" href="<?php echo $href; ?>"><?php echo $title; ?></a></h3>
			<p><?php echo $content?></p></li><!-- <p>Published: <?php echo $pubdate?></p></li> -->
		<?php
		}
	}
}
?>